<?php

Class log{


    private $file = "";
    private $time = 0;
    private $maxSizeFile = 1048576;
    
    function __construct($file, $fsize = 1048576){
        $this->file = $file;
        $this->maxSizeFile = $fsize;
    }    
    
    function debug($arr){
        if(@filesize($this->file) > $this->maxSizeFile){
            $fp = fopen($this->file, 'w');    
        }else{
            $fp = fopen($this->file, 'a');
        }
                
        if(is_array($arr)){
            foreach($arr as $key => $val){
                fwrite($fp, "[".$key."] => ");
                $this->debug($val);
            }
        }else{
            fwrite($fp, $arr."\n");
            fclose($fp);    
        }
    }        
    
    
    function start($query){
        
        $this->time = microtime();
        
        if(@filesize($this->file) > $this->maxSizeFile){
            $fp = fopen($this->file, 'w');    
        }else{
            $fp = fopen($this->file, 'a');
        }
                
        $text = "запрос : ".$query."  ";
        $text .= date("d.M.Y H:i:s")."\n";
        fwrite($fp, $text);
        fclose($fp);
    }
    
    function end(){
        $text = $this->sqlQueryes();
        $text .=  "Время : ". (microtime() - $this->time) ." s ; ";
        $text .=  "Память : ". (memory_get_usage()/ 1024) ." kb \n\n";
        $text .= "****************************\n";
        $fp = fopen($this->file, 'a+');
        fwrite($fp, $text);
    }
    
    private function sqlQueryes(){
        $text = "SQL запросы (".count(DBConnect::$queryes)." шт.) : \n";
        if(is_array(DBConnect::$queryes)) foreach(DBConnect::$queryes as $val){
            $text .= "".$val;
        }
        return $text;
    }
}
?>